
package chat.application.client;
import java.io.*;
import java.net.*;

import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.parsers.*;
import org.xml.sax.InputSource;
import org.w3c.dom.*;
import java.io.*;


/**
 *
 * @author warren
 */
public class ChatApplicationClient {

    
    public static void main(String[] args) {
        try
        {			
//                org.tempuri.ChatService service = new org.tempuri.ChatService();
//                org.tempuri.IChatService port = service.getBasicHttpBindingIChatService();
//
//                boolean result = port.login(null);
                uiMain main = new uiMain(true);
                main.setVisible(true);

        }
        catch(Exception e)
        {
                e.printStackTrace();
        }
    }
    
    //method accepts xml string and returns filled object depending on xml message
    public static void parseXmlMessage(String serverXml) {
       String xmlRecords =
        "<data>" +
        " <employee>" +
        "   <name>John</name>" +
        "   <title>Manager</title>" +
        " </employee>" +
        " <employee>" +
        "   <name>Sara</name>" +
        "   <title>Clerk</title>" +
        " </employee>" +
        "</data>";

      try {
          DocumentBuilderFactory docBuilderFactory =
              DocumentBuilderFactory.newInstance();
          DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
          InputSource inputSource = new InputSource();
          inputSource.setCharacterStream(new StringReader(xmlRecords));

          Document doc = docBuilder.parse(inputSource);
          NodeList nodes = doc.getElementsByTagName("employee");

          // iterate the employees
          for (int i = 0; i < nodes.getLength(); i++) {
             Element element = (Element) nodes.item(i);

             NodeList name = element.getElementsByTagName("name");
             Element line = (Element) name.item(0);
             System.out.println("Name: " + getCharacterDataFromElement(line));

             NodeList title = element.getElementsByTagName("title");
             line = (Element) title.item(0);
             System.out.println("Title: " + getCharacterDataFromElement(line));
          }
      }
      catch (Exception e) {
          e.printStackTrace();
      }
      /*
      output :
          Name: John
          Title: Manager
          Name: Sara
          Title: Clerk
      */    

    }

    public static String getCharacterDataFromElement(Element e) {
      Node child = e.getFirstChild();
      if (child instanceof CharacterData) {
         CharacterData cd = (CharacterData) child;
         return cd.getData();
      }
      return "?";
    }
  

    
}
